/*
 * Copyright (c) 2012, Markus Weber All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer. Redistributions in binary form must reproduce the
 * above copyright notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of the author nor the names of its contributors may be used to endorse or
 * promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */
package de.dfki.graph;

import java.util.HashMap;
import java.util.Map;

/**
 * Labeled vertex
 *
 * @author Markus Weber
 *
 * @param <T>
 */
public class LabeledVertex<T extends LabelType<?>> implements Vertex<T>,
        Comparable<LabeledVertex<T>> {

    /**
     * Serial id
     */
    private static final long serialVersionUID = 6446428315784935019L;
    /**
     * Label of the vertex.
     */
    protected T label;
    /**
     * Vertex ID
     */
    private final String ID;
    /**
     * Attributes stored for the vertex.
     */
    private Map<String, String> attributes = new HashMap<String, String>();

    /**
     * Creates a vertex.
     *
     * @param label
     * @param ID
     */
    LabeledVertex(T label, final String ID) {
        super();
        this.label = label;
        this.ID = ID;

    }

    /*
     * (non-Javadoc)
     *
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        LabeledVertex<?> other = (LabeledVertex<?>) obj;
        if (label == null) {
            if (other.label != null) {
                return false;
            }
        } else if (!label.equals(other.label)) {
            return false;
        }
        return true;
    }

    /*
     * (non-Javadoc)
     *
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */
    @Override
    public int compareTo(LabeledVertex<T> o) {
        if (o != null) {
            if (o.getType().getWeight() == this.getType().getWeight()) {
                return 0;
            }
            return (o.getType().getWeight() > this.getType().getWeight()) ? -1
                    : 1;
        }
        return 0;
    }

    /*
     * (non-Javadoc)
     *
     * @see de.dfki.structures.Vertex#getType()
     */
    @Override
    public T getType() {
        return label;
    }

    /**
     * Returns the vertex ID.
     *
     * @return
     */
    public final String getID() {
        return ID;
    }

    /*
     * (non-Javadoc)
     *
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "LabeledVertex [label=" + label + ", weight="
                + label.getWeight() + ", ID=" + ID + ", attributes="
                + this.attributes + "]";
    }

    /**
     * Returns the attribute map
     *
     * @return
     */
    public final Map<String, String> getAttributes() {
        return attributes;
    }

    /**
     * Adds an attribute.
     *
     * @param attribute
     * @param value
     */
    public void addAttribute(final String attribute, final String value) {
        this.attributes.put(attribute, value);
    }

    /**
     * Returns the value for an attribute.
     *
     * @param attribute
     * @return
     */
    public String getAttribute(final String attribute) {
        return this.attributes.get(attribute);
    }
}
